import { Ipc_ChannelEnum } from "@enum/channel.enum";
import { Ipc } from "@main/helper/ipcMain";
import { BrowserWindow } from "electron";
import { getWinById } from "@main/helper/handle";



export class DownLoad {
    
    constructor(private win:BrowserWindow,private url:string){
        this.downloadURL(url)
    }

    downloadURL(url:string):void{
        this.win.webContents.downloadURL(url)
        this.willDwonLoad()
    }

    willDwonLoad():void{
        this.win.webContents.session.on('will-download', (event, item, webContents) => {
            //打印文件名 不包含文件夹
            console.log(item.getFilename())
        })
    }

}

export class DownLoadFileService {

    static downLoadUrl():void{
        Ipc.on<[BrowserWindow,string]>(Ipc_ChannelEnum.DOWNLOAD_FILE,(_,win,url)=>{
            new DownLoad(win,url)
        })
    }

    static downLoadUrlById():void{

        Ipc.on<[ string ,number ]>(Ipc_ChannelEnum.DOWNLOAD_FILE_BYID,(_,url,id)=>{

            const win = getWinById(id)
            win && new DownLoad(win,url)
        })
    }

    static on():void{
        this.downLoadUrl()
        this.downLoadUrlById()
    }
}
